/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.umb.MAS.Models.DAOs;

import co.umb.MAS.Models.conexionDbPool;
import co.umb.MAS.Models.PersonaModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Practica
 */
public class PostgresPersonaDao implements AccionesDao {

    private Connection cnn = null;

    @Override
    public ArrayList<PersonaModel> consultarTodos() {
        ArrayList<PersonaModel> pacientes = new ArrayList<>();
        try {
            cnn = conexionDbPool.obtenerConexion();
            String query = "select id_persona,nombre,apellido_1,apellido_2,"
                           + "numero_id from t_persona";
            PreparedStatement ps = cnn.prepareStatement(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                PersonaModel pr = new PersonaModel();
                pr.setIdPersona(rs.getInt(1));
                pr.setNombre(rs.getString(2));
                pr.setApellido1(rs.getString(3));
                pr.setApellido2(rs.getString(4));
                pr.setNumeroID(rs.getString(5));
                pacientes.add(pr);
            }
            cnn.close();
        } catch (SQLException ex) {
            Logger.getLogger(PostgresPersonaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pacientes;
    }

    @Override
    public Object conultar(String criterio) {
        PersonaModel p = null;
        try {
            cnn = conexionDbPool.obtenerConexion();
            ResultSet res;            
            String query="select * from t_persona where id_persona="+Integer.parseInt(criterio);            
            PreparedStatement ps = cnn.prepareStatement(query);
            res = ps.executeQuery();
            if(res.next()){
                p = new PersonaModel();
                p.setIdPersona(res.getInt(1));
                p.setNombre(res.getString(2));
                p.setApellido1(res.getString(3));
                p.setApellido2(res.getString(4));
                p.setTipoId(res.getInt(5));
                p.setNumeroID(res.getString(6));
                p.setId_tipo_persona(res.getInt(7));
                p.setEstado(res.getInt(8));
                p.setId_genero(res.getInt(9));
                p.setId_rh(res.getInt(10));
                p.setFecha_nacimiento(res.getDate(11));    
                return p;
            }
            cnn.close();
        } catch (SQLException ex) {
            Logger.getLogger(PostgresPersonaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return p;
    }

    public String consultarPorCedula(String cedula){
        PersonaModel p = null;
        try {
            cnn = conexionDbPool.obtenerConexion();
            ResultSet res;            
            String query="select * from t_persona where numero_id='"+cedula+"'";            
            PreparedStatement ps = cnn.prepareStatement(query);
            res = ps.executeQuery();
            if(res.next()){
                p = new PersonaModel();
                p.setIdPersona(res.getInt(1));
                p.setNombre(res.getString(2));
                p.setApellido1(res.getString(3));
                p.setApellido2(res.getString(4));
                p.setTipoId(res.getInt(5));
                p.setNumeroID(res.getString(6));
                p.setId_tipo_persona(res.getInt(7));
                p.setEstado(res.getInt(8));
                p.setId_genero(res.getInt(9));
                p.setId_rh(res.getInt(10));
                p.setFecha_nacimiento(res.getDate(11));    
                return p.toString();
            }
            cnn.close();
        } catch (SQLException ex) {
            Logger.getLogger(PostgresPersonaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    @Override
    public boolean crear(Object item) {
        boolean successful = false;
        try {
            cnn = conexionDbPool.obtenerConexion();
            PersonaModel paciente = (PersonaModel) item;
            paciente.setId_tipo_persona(3); 
            
            Calendar c = Calendar.getInstance();
            c.setTime(paciente.getFecha_nacimiento());
            int mes = c.get(Calendar.MONTH);
            mes = mes==0? 12 : mes;
            
            String query = "insert into t_persona values (nextval('persona_id_persona_seq')"
                    + ",'" + paciente.getNombre() + "','" + paciente.getApellido1()
                    + "','" + paciente.getApellido2() + "'," + paciente.getTipoId()
                    + ",'" + paciente.getNumeroID() + "'," + paciente.getId_tipo_persona()
                    + "," + paciente.getEstado() + "," + paciente.getId_genero()
                    + ",'" + paciente.getId_rh() + "','" + c.get(Calendar.YEAR)
                    +"-"+mes
                    +"-"+c.get(Calendar.DAY_OF_MONTH)+"')";
            PreparedStatement ps = cnn.prepareStatement(query);
            successful = ps.executeUpdate() == 1 ? true : false;
            cnn.close();
        } catch (SQLException ex) {
            Logger.getLogger(PostgresPersonaDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return successful;
    }

    @Override
    public void eliminar() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void editar() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
